import tkinter as tk
from tkinter import ttk
from datetime import date, timedelta
import ExcelDataLoader

class TransportManagementSystem:
    def __init__(self):
        # 初始化GUI窗口及其组件
        self.root = tk.Tk()
        self.root.title("云锡1360运输区信息管理系统v1.0")  # 设置窗��标题
        self.root.geometry("1024x768")  # 设置窗口大小
        tk.Label(self.root, text="欢迎使用云锡1360运输区信息管理系统", font=("Arial", 20)).pack(pady=20)  # 显示欢迎信息
        self.root.iconbitmap("main.ico")  # 设置窗口图标
        self.root.attributes("-alpha", 0.99)  # 设置窗口透明度

        # 获取当前日期的前一天
        yesterday = date.today() - timedelta(days=1)
        day = yesterday.day
        month = yesterday.month
        year = yesterday.year

        # 创建一个框架用于放置顶部的控件
        top_frame = tk.Frame(self.root)
        top_frame.pack(side=tk.TOP, fill=tk.X, pady=5)

        # 添加下拉框用于选择日期
        self.day_var = tk.StringVar(value=str(day))
        self.month_var = tk.StringVar(value=str(month))
        self.year_var = tk.StringVar(value=str(year))

        days = [str(i) for i in range(1, 32)]
        months = [str(i) for i in range(1, 13)]
        years = [str(i) for i in range(2000, 2031)]

        # 美化下拉框和标签

        tk.OptionMenu(top_frame, self.year_var, *years).pack(side=tk.LEFT)
        tk.Label(top_frame, text="年", font=("Arial", 12)).pack(side=tk.LEFT, padx=5)

        tk.OptionMenu(top_frame, self.month_var, *months).pack(side=tk.LEFT)
        tk.Label(top_frame, text="月", font=("Arial", 12)).pack(side=tk.LEFT, padx=5)

        tk.OptionMenu(top_frame, self.day_var, *days).pack(side=tk.LEFT)
        tk.Label(top_frame, text="日", font=("Arial", 12)).pack(side=tk.LEFT, padx=5)



        # 在顶部框架中添加按钮并美化
        data = tk.Button(top_frame, text="刷新数据", command=self.get_data, font=("Arial", 12), bg="lightblue", fg="black")
        data.pack(side=tk.LEFT, padx=10)

        # 在底部添加退出按钮并美化
        # btn = tk.Button(self.root, text="退出", command=self.root.quit)
        # btn.config(bg="red", fg="white", font=("Arial", 16, "bold"), padx=20, pady=10, bd=5, relief=tk.RAISED)
        # btn.pack(fill=tk.X, side=tk.BOTTOM, pady=20)

        # 创建一个框架用于放置表格
        table_frame = tk.Frame(self.root)
        table_frame.pack(fill=tk.BOTH, expand=True, padx=10,pady=10)

        # 创建表格
        columns = ("溜井", "进矿量", "运矿量", "查井数","备注")
        self.tree = ttk.Treeview(table_frame, show="headings", columns=columns)
        self.tree.column("溜井", width=200,anchor="center")
        self.tree.column("进矿量", width=100,anchor="center")
        self.tree.column("运矿量", width=100,anchor="center")
        self.tree.column("查井数", width=100,anchor="center")

        self.tree.column("备注", width=300)

        # 设置表头样式
        style = ttk.Style()
        style.configure("Treeview.Heading", font=("Arial", 12, "bold"), foreground="blue")

        # 设置表头
        for col in columns:
            self.tree.heading(col, text=col)
        self.tree.pack(fill=tk.BOTH, expand=True)

        # Configure Treeview style
        style = ttk.Style()
        style.configure("Treeview", font=("Arial", 12), rowheight=25)
        style.configure("Treeview.Heading", font=("Arial", 14, "bold"), foreground="blue")
        style.layout("Treeview", [('Treeview.treearea', {'sticky': 'nswe'})])

        # Add grid lines
        style.configure("Treeview", borderwidth=1, relief="solid", highlightthickness=1, highlightbackground="black")

        # Add alternating row colors
        self.tree.tag_configure('oddrow', background='lightgrey')
        self.tree.tag_configure('evenrow', background='white')

        self.root.mainloop()

    # 点击了刷新数据按钮
    def get_data(self):
        selected_date = f"{self.day_var.get()}日"
        print(f"Selected date: {selected_date}")

        # Clear existing data in the Treeview
        for item in self.tree.get_children():
            self.tree.delete(item)


        # Insert each column as a new row
        loader = ExcelDataLoader.ExcelDataLoader("C:/Users/张旭东/Downloads/生产报表.xlsx")

        df = loader.get_data(selected_date)

        # nan替换空值
        df = df.fillna('')

        if len(df) >= 2:
            df.iloc[-2] = df.iloc[-2].astype(str) +" " + df.iloc[-1].astype(str)
            df = df[:-1]
        # Insert each column as a new row, including the header
        for i, col in enumerate(df.columns):
            row_data = [col] + df[col].tolist()
            tag = 'evenrow' if i % 2 == 0 else 'oddrow'
            self.tree.insert("", "end", values=row_data, tags=(tag,))


if __name__ == '__main__':
    app = TransportManagementSystem()